/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
         vector<vector<int>> ret;
        if(root==nullptr)
        {
            return ret;
        }
        queue<TreeNode*> q;
        q.push(root);
        int size=1;

        while(size)
        {
            vector<int> v;
            while(size--)
            {
                auto t=q.front();
                q.pop();
                v.push_back(t->val);

                if(t->left)
                q.push(t->left);

                if(t->right)
                q.push(t->right);

            }
            size=q.size();
            ret.push_back(v);
        }
        reverse(ret.begin(),ret.end());
        return ret;
    }
};